<?php
/*******************************************************************************
* [LQBCMS] LQBCMS
* @Copyright (C) 2014-2015  http://www.liqingbo.cn   All rights reserved.
* @Team  liqingbo.cn
* @Author: 码农 QQ:252588119
* @Licence http://www.liqingbo.cn/license.txt
*******************************************************************************/


function get_info($id=0,$table=''){
    if(empty($id)) return false;
    $table = !empty($table) ? $table : CONTROLLER_NAME;
    $Module = M($table);
    $info = $Module->where( array('id'=>$id) )->find();
    return $info;
}

function get_product_code(){
    $Product = M('Product');
    $endId = $Product->order('id DESC')->getField('id');
    $nextId = $endId+1;
    $str = 'JW'.str_pad($nextId,8,'0',STR_PAD_LEFT);
    return $str;
}

function get_rank_list(){
    $list = M('rank')->where()->order('sort ASC,id DESC')->limit()->select();
    return $list;
}

function get_rank_name($id){
   return M('rank')->where( array('id'=>$id) )->getField('name');
}

function get_level_list(){
    $list = M('level')->where()->order('sort ASC,id DESC')->limit()->select();
    return $list;
}

function get_level_name($id){
   return M('level')->where( array('id'=>$id) )->getField('name');
}


function get_counselor_list(){
    $list = M('Counselor')->where()->order('sort ASC,id DESC')->limit()->select();
    return $list;
}

function get_counselor_name($id){
   return M('Admin')->where( array('id'=>$id) )->getField('username');
}

function get_source_list(){
    $list = M('Source')->where()->order('sort ASC,id DESC')->limit()->select();
    return $list;
}

function get_source_name($id){
   return M('Source')->where( array('id'=>$id) )->getField('name');
}

function get_course_name($id){
   return M('Course')->where( array('id'=>$id) )->getField('name');
}

function get_course_list(){
    return M('Course')->order('sort ASC')->select();
}

function get_course_info($uid=0, $field=''){
    $info = M('Course')->where( array('id'=>$uid) )->find();
    if(!empty($field)) return $info[$field];
    return $info;
}


function get_partner_list(){
    return M('Partner')->order('sort ASC')->select();
}

function get_partner_info($uid=0, $field=''){
    $info = M('Partner')->where( array('id'=>$uid) )->find();
    if(!empty($field)) return $info[$field];
    return $info;
}




function house_status_icon($id=0){
    switch ($id) {
        case 37:
            $icon = '<i class="selstag2"></i>'; //待售
            break;
        case 38:
            $icon = '<i class="selstag2"></i>'; //在售
            break;
        case 39:
            $icon = '<i class="selstag3"></i>'; //尾盘
            break;
        case 40:
            $icon = '<i class="selstag4"></i>'; //售完
            break;
        default:
            $icon = '1';
            break;
    }
    return $icon;
}



function product_category_list($id=0, $field=''){
    $ProductCategory = M('ProductCategory');
    if(!empty($id)){
        $info = $ProductCategory->where('id='.$id)->find();
        if(!empty($field)){
            return $info[$field];
        }else{
            return $info;
        }
    }
    $list = $ProductCategory->where()->order('sort ASC')->select();
    return $list;
}

function product_category_info($id,$field=''){
    $ProductCategory = M('ProductCategory');
    $map['id'] = $id;
    $info = $ProductCategory->where($map)->find();
    if(!empty($field)){
        return $info[$field];
    }
    return $info;
}

function product_texture_list($id=0, $field=''){
    $ProductTexture = M('ProductTexture');
    if(!empty($id)){
        $info = $ProductTexture->where('id='.$id)->find();
        if(!empty($field)){
            return $info[$field];
        }else{
            return $info;
        }
    }
    $list = $ProductTexture->where()->order('sort ASC')->select();
    return $list;
}

function product_price_list(){
    $ProductPrice = M('ProductPrice');
    $list = $ProductPrice->where()->order('sort ASC')->select();
    return $list;
}



function getClassifyList($pid=''){
    $Classify = M('Classify');
    if($pid!=''){
        $map['pid'] = $pid;
    }
    $list = $Classify->where($map)->order('sort ASC')->select();
    return $list;
}

function getClassifyInfo($id='', $field=''){
    $Classify = M('Classify');
    $map['id'] = $id;
    $info = $Classify->where($map)->find();
    if(!empty($field)){
        return $info[$field];
    }
    return $info;
}

function provinceList($limit=10,$order='sort'){
    $Area = M('Area');
    $map['pid'] = 0;
    $list = $Area->where($map)->order()->limit()->select();
    return $list;
}

function cityList($pid=0){
    $Area = M('Area');
    $map['pid'] = $pid;
    $map['cid'] = 0;
    $list = $Area->where($map)->order()->limit()->select();
    return $list;
}

function areaList($cid=0){
    $Area = M('Area');
    $map['cid'] = $cid;
    $list = $Area->where($map)->order()->limit()->select();
    return $list;
}

function getAreaInfo($id,$field=''){
    $Area = M('Area');
    $map['id'] = $id;
    $info = $Area->where($map)->find();
    if(!empty($field)){
        return $info[$field];
    }
    return $info;
}



function province_list($limit=10,$order='sort'){
    $Area = M('Area');
    $map['pid'] = 0;
    $list = $Area->where($map)->order()->limit()->select();
    return $list;
}

function city_list($pid=0){

    if(empty($pid)) return false;
    $Area = M('Area');
    $map['pid'] = $pid;
    $map['cid'] = 0;
    $list = $Area->where($map)->order()->limit()->select();
    return $list;
}

function area_list($cid=0){
    $Area = M('Area');
    $map['cid'] = $cid;
    $list = $Area->where($map)->order()->limit()->select();
    return $list;
}


function area_name($id){
   return M('Area')->where( array('id'=>$id) )->getField('name');
}



function get_role_list(){
    return M('Role')->order('sort ASC')->select();
}

function get_role_name($id){
    return M('Role')->where( array('id'=>$id) )->getField('name');
}

function get_admin_list(){
    $map['status'] = 1;
    return M('Admin')->where( $map )->order('role_id ASC')->select();
}

function get_admin_info($uid=0, $field=''){
    $info = M('Admin')->where( array('id'=>$uid) )->find();
    if(!empty($field)) return $info[$field];
    return $info;
}


function selected($id,$value){
    $str = 'selected=""';
    if($id==$value){
        return $str;
    }
}

function checked($id,$value){
    $str = 'checked=""';
    if($id==$value){
        return $str;
    }
}

//逆向获取ID，根据分类名获取对应的分类ID
function get_course_id($name=''){
    return M('Course')->where( array('name'=>$name) )->getField('id');
}
function get_partner_id($name=''){
    return M('Partner')->where( array('name'=>$name) )->getField('id');
}
function get_source_id($name=''){
    return M('Source')->where( array('name'=>$name) )->getField('id');
}
function get_level_id($name=''){
    return M('Level')->where( array('name'=>$name) )->getField('id');
}
function get_rank_id($name=''){
    return M('Rank')->where( array('name'=>$name) )->getField('id');
}



/**
 * 数组转xls格式的excel文件
 * @param  array  $data      需要生成excel文件的数组
 * @param  string $filename  生成的excel文件名
 *      示例数据：
        $data = array(
            array(NULL, 2010, 2011, 2012),
            array('Q1',   12,   15,   21),
            array('Q2',   56,   73,   86),
            array('Q3',   52,   61,   69),
            array('Q4',   30,   32,    0),
           );
 */
function create_xls($data,$filename='simple.xls'){
    ini_set('max_execution_time', '0');
    Vendor('PHPExcel.PHPExcel');
    $filename=str_replace('.xls', '', $filename).'.xls';
    $phpexcel = new PHPExcel();
    $phpexcel->getProperties()
        ->setCreator("Maarten Balliauw")
        ->setLastModifiedBy("Maarten Balliauw")
        ->setTitle("Office 2007 XLSX Test Document")
        ->setSubject("Office 2007 XLSX Test Document")
        ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
        ->setKeywords("office 2007 openxml php")
        ->setCategory("Test result file");
    $phpexcel->getActiveSheet()->fromArray($data);
    $phpexcel->getActiveSheet()->setTitle('Sheet1');
    $phpexcel->setActiveSheetIndex(0);
    header('Content-Type: application/vnd.ms-excel');
    header("Content-Disposition: attachment;filename=$filename");
    header('Cache-Control: max-age=0');
    header('Cache-Control: max-age=1');
    header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
    header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
    header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
    header ('Pragma: public'); // HTTP/1.0
    $objwriter = PHPExcel_IOFactory::createWriter($phpexcel, 'Excel5');
    $objwriter->save('php://output');
    exit;
}

/**
 * 数据转csv格式的excle
 * @param  array $data      需要转的数组
 * @param  string $header   要生成的excel表头
 * @param  string $filename 生成的excel文件名
 *      示例数组：
        $data = array(
            '1,2,3,4,5',
            '6,7,8,9,0',
            '1,3,5,6,7'
            );
        $header='用户名,密码,头像,性别,手机号';
 */
function create_csv($data,$header=null,$filename='simple.csv'){
    // 如果手动设置表头；则放在第一行
    if (!is_null($header)) {
        array_unshift($data, $header);
    }
    // 防止没有添加文件后缀
    $filename=str_replace('.csv', '', $filename).'.csv';
    ob_clean();
    Header( "Content-type:  application/octet-stream ");
    Header( "Accept-Ranges:  bytes ");
    Header( "Content-Disposition:  attachment;  filename=".$filename);
    foreach( $data as $k => $v){
        // 如果是二维数组；转成一维
        if (is_array($v)) {
            $v=implode(',', $v);
        }
        // 替换掉换行
        $v=preg_replace('/\s*/', '', $v);
        // 解决导出的数字会显示成科学计数法的问题
        $v=str_replace(',', "\t,", $v);
        // 转成gbk以兼容office乱码的问题
        echo iconv('UTF-8','GBK',$v)."\t\r\n";
    }
}

/**
 * 导入excel文件
 * @param  string $file excel文件路径
 * @return array        excel文件内容数组
 */
function import_excel($file){
    // 判断文件是什么格式
    $type = pathinfo($file);
    $type = strtolower($type["extension"]);
    $type=$type==='csv' ? $type : 'Excel5';
    ini_set('max_execution_time', '0');
    Vendor('PHPExcel.PHPExcel');
    // 判断使用哪种格式
    $objReader = PHPExcel_IOFactory::createReader($type);
    $objPHPExcel = $objReader->load($file);
    $sheet = $objPHPExcel->getSheet(0);
    // 取得总行数
    $highestRow = $sheet->getHighestRow();
    // 取得总列数
    $highestColumn = $sheet->getHighestColumn();
    //循环读取excel文件,读取一条,插入一条
    $data=array();
    //从第一行开始读取数据
    for($j=1;$j<=$highestRow;$j++){
        //从A列读取数据
        for($k='A';$k<=$highestColumn;$k++){
            // 读取单元格
            $data[$j][]=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue();
        }
    }
    return $data;
}


?>